//登录
<template>
  <div class="login">
    <div class="left">
      <p>非学无以广才，</p>
      <p>非志无以成学</p>
      <img src="../assets/29.png" alt="" />
    </div>
    <div class="right">
      <p class="title">考试网登录</p>
      <p class="post">密码登录</p>
      <el-form ref="form" :model="form">
        <el-form-item>
          <img src="../assets/5.png" alt="" /><el-input
            v-model="form.userName"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <img src="../assets/mima.png" alt="" /><el-input
            placeholder="请输入密码"
            v-model="form.password"
            show-password
          ></el-input>
        </el-form-item>
        <div class="autoLogin">
          <el-checkbox v-model="checked" style="color: #a0a0a0"
            >一周内自动登录</el-checkbox
          >
          <span
            @click="clearCookie"
            style="
              cursor: pointer;
              color: #f19149;
              font-size: 0.75rem;
              margin-left: 5px;
            "
            >忘记密码？</span
          >
        </div>
        <el-form-item>
          <el-button type="primary" @click="submitForms()" class="button"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      form: {
        userName: "",
        password: "",
      },
      checked: true,
    };
  },
  methods: {
    //点击登录调用方法
    submitForms() {
      //保存的账号
      var name = this.form.userName;
      //保存的密码
      var pass = this.form.password;
      if (name == "" || name == null) {
        this.$message.error("请输入正确的用户名");
        return;
      } else if (pass == "" || pass == null) {
        this.$message.error("请输入正确的密码");
        return;
      } else {
        localStorage.removeItem("Authorization");
        //  this.$router.push("/Index")
        // console.log(name)
        this.$router.push("/Index");
      }
      if (this.checked == true) {
        this.setCookie(name, pass, 7);
      }
      //接口
      //   var url='myserver/user/login';
      //   this.$http.post(url,this.form,{emulateJSON:true})
      //    .then(res=>{
      // if(this.body=="fail"){
      //  alert("用户名或密码错误,请重新输入");
      //  this.form.userName='';
      //  this.form.password='';
      //  return
      // } else{
      // // alert（"登陆成功！"）
      //  this.$router.push("/index")
      // }
      //  });
    },
    //设置cookie
    setCookie(c_name, c_pwd, exdays) {
      var exdate = new Date(); //获取时间
      exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //保存的天数
      //字符串拼接cookie
      window.document.cookie =
        "userName" + "=" + c_name + ";path=/;expires=" + exdate.toGMTString();
      window.document.cookie =
        "userPwd" + "=" + c_pwd + ";path=/;expires=" + exdate.toGMTString();
    },
    //读取cookie
    getCookie: function () {
      if (document.cookie.length > 0) {
        var arr = document.cookie.split("; "); //这里显示的格式需要切割一下自己可输出看下
        for (var i = 0; i < arr.length; i++) {
          var arr2 = arr[i].split("="); //再次切割
          //判断查找相对应的值
          if (arr2[0] == "userName") {
            this.form.userName = arr2[1]; //保存到保存数据的地方
          } else if (arr2[0] == "userPwd") {
            this.form.password = arr2[1];
          }
        }
      }
    },
    //清除cookie
    clearCookie: function () {
      this.setCookie("", "", -1); //修改2值都为空，天数为负1天就好了
      this.$router.push("/Forget")
    },
  },
  //页面加载调用获取cookie值
  mounted() {
    this.getCookie(); // chrome
    document.body.scrollTop = 0;
    // firefox
    document.documentElement.scrollTop = 0;
    // safari
    window.pageYOffset = 0;
    localStorage.setItem("get",0)
  },
};
</script>
<style lang="scss" scoped>
.login {
  min-width: 1366px;
  width: 100%;
  background: url("../assets/500813050.png") no-repeat left top;
  background-size: cover;
  -webkit-background-size: cover;
  -o-background-size: cover;
  height: 100%;
  position: fixed;
  display: flex;
  justify-self: unset;
  justify-content: space-around;
  flex-wrap: wrap;
  .left {
    width: 723px;
    color: #fff;
    font-size: 35px;
    text-align: center;
    // background: olivedrab;
    margin-top: 16%;
    p {
      width: 100%;
      margin: 0 auto;
      line-height: 60px;
    }
    p:nth-child(2) {
      text-indent: 8rem;
    }
    img {
      width: 723px;
      margin-top: 40px;
    }
  }
  .right {
    margin-top: 9%;
    width: 480px;
    height: 589px;
    background: #ffffff;
    box-shadow: 0px 30px 28px 4px rgba(8, 2, 3, 0.17);
    border-radius: 20px;
    .title {
      color: #1269ff;
      font-size: 31px;
      line-height: 31px;
      text-align: center;
      margin-top: 48px;
      margin-bottom: 77px;
    }
    .post {
      width: 14%;
      margin: 20px 0px 20px 11%;
      // text-indent: 1rem;
      line-height: 40px;
      border-bottom: 2px solid #1269ff;
    }
    .el-form {
      width: 80%;
      margin: 0 auto;
      margin-top: 40px;
      .el-form-item {
        width: 100%;
        position: relative;
        img {
          top: 0;
          bottom: 0;
          margin: auto;
          left: 10px;
          position: absolute;
          z-index: 123;
        }
        .el-input {
          width: 100%;
          /deep/ .el-input__inner {
            // background: #1269FF;
            text-indent: 2rem;
            // padding-left: 20px;
            // padding: 0 10px !important;
          }
          // background: #1269FF !important;
        }
        button {
          width: 90%;
          margin: 0 auto;
          margin-left: 5%;
          margin-top: 80px;
        }
      }
    }
  }
}

.autoLogin {
  display: flex;
  justify-content: space-between;
}
</style>